package com.shunbo.convenient.controller;

import com.shunbo.convenient.resultVo.OrdersVo;
import com.shunbo.convenient.service.OrdersService;
import com.shunbo.convenient.service.ProductOrderService;
import com.shunbo.convenient.service.ProductService;
import com.shunbo.convenient.service.StoreService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.Orders;
import com.shunbo.convenient.vo.Product;
import com.shunbo.convenient.vo.ProductOrder;
import com.shunbo.convenient.vo.Store;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author weizhuang
 * 财务
 */
@RestController
@RequestMapping("/finance")
public class FinanceController {


    @Resource
    private OrdersService ordersService;
    @Resource
    private StoreService storeService;
    @Resource
    private ProductOrderService productOrderService;
    @Resource
    private ProductService productService;


    /**
     * 财务列表
     *
     * @param orders 小区vo
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getFinanceList(@RequestBody Orders orders) {
        try {
            List<OrdersVo> list = new ArrayList<>();
            List<Orders> all = ordersService.findAll(orders);
            for (Orders orders1: all
                 ) {
                Double money = 0.0;
                OrdersVo ordersVo = new OrdersVo();
                ordersVo.setOrders(orders1);
                Store byId = storeService.findById(orders1.getSid() + "");
                ordersVo.setStoreName(byId.getName());
                ProductOrder productOrder = new ProductOrder();
                productOrder.setOid(orders1.getOrderId());
                List<ProductOrder> all1 = productOrderService.findAll(productOrder);
                for (ProductOrder po: all1
                ) {
                    Product pro = productService.findById(po.getPid() + "");
                    money+= po.getNum()*pro.getMoney();
                }
                ordersVo.setMoney(money+"");
                list.add(ordersVo);
            }
            return ConvenientResult.build(200, "成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }




}
